package com.leinao.media.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.leinao.media.dto.MediaThermalCameraAlarmDTO;
import com.leinao.media.entity.*;
import com.leinao.media.service.MediaService;
import com.leinao.media.service.MediaThermalCameraAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流媒体Controller
 *
 * @author zhangyu
 * @date 2021/12/1 15:54
 */
@RestController
@RequestMapping("/media")
public class MediaController {

    @Autowired
    private MediaService mediaService;

    @Autowired
    private MediaThermalCameraAlarmService alarmService;

    /**
     * 设备激活
     *
     * @param param 设备基础查询参数
     * @return Result
     */
    @PostMapping("/activateDevice")
    public Result<Void> activateDevice(@RequestBody BaseDeviceParam param) {
        mediaService.activateDevice(param);
        return Result.ok();
    }

    /**
     * 设备注册
     *
     * @param param 设备基础查询参数
     * @return Result
     */
    @PostMapping("/login")
    public Result<Long> login(@RequestBody BaseDeviceParam param) {
        return Result.ok(mediaService.login(param));
    }

    /**
     * 设备登出
     *
     * @param param 登出参数
     * @return Result
     */
    @PostMapping("/logout")
    public Result<Void> logout(@RequestBody LogoutParam param) {
        mediaService.logout(param);
        return Result.ok();
    }

    /**
     * 获取设备状态
     *
     * @param param 设备基础查询参数
     * @return Result
     */
    @PostMapping("/getDeviceStatus")
    public Result<String> getDeviceStatus(@RequestBody BaseDeviceParam param) {
        return Result.ok(mediaService.getDeviceStatus(param));
    }

    /**
     * 获取设备下面的摄像机列表
     *
     * @param param 设备基础查询参数
     * @return Result
     */
    @PostMapping("/getCameraList")
    public Result<List<Camera>> getCameraList(@RequestBody BaseDeviceParam param) {
        return Result.ok(mediaService.getCameraList(param));
    }

    /**
     * 抓图
     *
     * @param param 抓图参数
     * @return Result
     */
    @PostMapping("/captureJpeg")
    public Result<String> captureJpeg(@RequestBody CaptureJpegParam param) {
        return Result.ok(mediaService.captureJpeg(param));
    }

    /**
     * 抓图_新
     *
     * @param param 抓图参数
     * @return Result
     */
    @PostMapping("/captureJpegNew")
    public Result<String> captureJpegNew(@RequestBody CaptureJpegParam param) {
        return Result.ok(mediaService.captureJpegNew(param));
    }

    /**
     * 抓图_返回图片流
     *
     * @param param 抓图参数
     * @return Result
     */
    @PostMapping("/captureJpegStream")
    public Result<String> captureJpegAsync(@RequestBody CaptureJpegParam param) {
        return Result.ok(mediaService.captureJpegStream(param));
    }

    /**
     * 云台控制
     *
     * @param param 云台控制参数
     * @return Result
     */
    @PostMapping("/cloudPlatformControl")
    public Result<Void> cloudPlatformControl(@RequestBody CloudPlatformControlParam param) {
        int category = param.getCategory();
        // 枪机
        if (2 == category) {
            return Result.fail(-2, "设备不支持");
        }
        String msg = mediaService.cloudPlatformControl(param);
        if (StrUtil.isNotBlank(msg)) {
            return Result.fail(-2, msg);
        }
        return Result.ok();
    }

    /**
     * 预置位设置
     *
     * @param param 预置位参数
     * @return Result
     */
    @PostMapping("/presetLocation")
    public Result<Void> setPresetLocation(@RequestBody PresetLocationParam param) {
        mediaService.setPresetLocation(param);
        return Result.ok();
    }

    /**
     * 月历查询
     *
     * @param param 录像查询参数
     * @return Result
     */
    @PostMapping("/getVideoMonth")
    public Result<JSONObject> getVideoMonth(@RequestBody VideoQueryParam param) {
        return Result.ok(mediaService.getVideoMonth(param));
    }

    /**
     * 录像文件查询
     *
     * @param param 录像查询参数
     * @return Result
     */
    @PostMapping("/findVideoFile")
    public Result<JSONObject> findVideoFile(@RequestBody VideoQueryParam param) {
        return Result.ok(mediaService.findVideoFile(param));
    }

    /**
     * 录像文件下载(通过时间)
     *
     * @param param 录像查询参数
     * @return Result
     */
    @PostMapping("/downloadVideoFileByTime")
    public Result<JSONObject> downloadVideoFileByTime(@RequestBody VideoDownloadParam param) {
        return Result.ok(mediaService.downloadVideoFileByTime(param));
    }

    /**
     * 获取下载进度
     *
     * @param param 下载进度获取参数
     * @return Result
     */
    @PostMapping("/getDownLoadProgress")
    public Result<Integer> getDownLoadProgress(@RequestBody DownLoadProgressParam param) {
        return Result.ok(mediaService.getDownLoadProgress(param));
    }

    /**
     * 停止下载
     *
     * @param param 录像查询参数
     * @return Result
     */
    @PostMapping("/stopDownloadVideoFile")
    public Result<Void> stopDownloadVideoFile(@RequestBody VideoQueryParam param) {
        mediaService.stopDownloadVideoFile(param);
        return Result.ok();
    }

    /**
     * 开始录像
     *
     * @param param 录像查询参数
     * @return Result
     */
    @PostMapping("/startMakeVideo")
    public Result<JSONObject> startMakeVideo(@RequestBody VideoQueryParam param) {
        return Result.ok(mediaService.startMakeVideo(param));
    }

    /**
     * 停止录像
     *
     * @param param 录像查询参数
     * @return Result
     */
    @PostMapping("/stopMakeVideo")
    public Result<String> stopMakeVideo(@RequestBody VideoQueryParam param) {
        return Result.ok(mediaService.stopMakeVideo(param));
    }

    /**
     * 获取设备硬盘详情
     *
     * @param param 查询参数
     * @return Result
     */
    @PostMapping("/getDeviceDiskInfo")
    public Result<JSONObject> getDeviceDiskInfo(@RequestBody BaseDeviceParam param) {
        return Result.ok(mediaService.getDeviceDiskInfo(param));
    }

    /**
     * 获取录像回放地址
     *
     * @param param 录像回放参数
     * @return Result
     */
    @PostMapping("/getPlayBackAddress")
    public Result<String> getPlayBackAddress(@RequestBody VideoPlayBackParam param) {
        return Result.ok(mediaService.getPlayBackAddress(param));
    }


    /*************************************以下是热成像相机特有接口**********************************************/

    /**
     * 设置测温规则
     *
     * @param param 测温规则参数
     * @return Result
     */
    @PostMapping("/setTemperatureRule")
    public Result<Void> setTemperatureRule(@RequestBody TemperatureRuleParam param) {
        mediaService.setTemperatureRule(param);
        return Result.ok();
    }

    /**
     * 开启布防
     *
     * @param param 设备基础查询参数
     * @return Result
     */
    @PostMapping("/setUpAlarm")
    public Result<Integer> setUpAlarm(@RequestBody BaseDeviceParam param) {
        return Result.ok(mediaService.setUpAlarm(param));
    }

    /**
     * 撤销布防
     *
     * @param param 撤销布防参数
     * @return Result
     */
    @PostMapping("/closeAlarmChan")
    public Result<Boolean> closeAlarmChan(@RequestBody CloseAlarmChanParam param) {
        return Result.ok(mediaService.closeAlarmChan(param));
    }

    /**
     * 获取红外告警信息
     *
     * @param param 红外报警查询条件
     * @return Result
     */
    @PostMapping("/getAlarmInfoList")
    public Result<Pagination<MediaThermalCameraAlarmDTO>> getAlarmInfoList(@RequestBody CameraAlarmQueryParam param) {
        return Result.ok(alarmService.queryList(param));
    }

    /**
     * 开启实时测温
     *
     * @param ip  红外相机ip
     * @param uid 用户id
     * @return Result
     */
    @GetMapping("/setUpThermometry")
    public Result<Integer> setUpThermometry(@RequestParam String ip, @RequestParam int uid) {
        return Result.ok(mediaService.setUpThermometry(ip, uid));
    }

    /**
     * 关闭实时测温
     *
     * @param handle 测温句柄
     * @return Result
     */
    @GetMapping("/closeThermometry")
    public Result<Boolean> closeThermometry(@RequestParam int handle) {
        return Result.ok(mediaService.closeThermometry(handle));
    }

}
